package com.heima.admin.controller;


import com.heima.admin.entity.AdSensitive;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exceptions.LeadnewsException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.dtos.SensitiveDto;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 前端控制器
 * </p>
 *
 * @author HM
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/api/v1/sensitive")
public class AdSensitiveController {

    @Autowired
    private AdSensitiveService adSensitiveService;

    @PostMapping("/list")
    public PageResult<SensitiveDto> findByPage(@RequestBody SensitiveDto sensitiveDto){

        return adSensitiveService.findByPage(sensitiveDto);
    }


    @PostMapping("/save")
    public ResponseResult saveSensitive(@RequestBody AdSensitive adSensitive){
        adSensitive.setCreatedTime(new Date());
        boolean b = adSensitiveService.save(adSensitive);
        if (!b){
            throw new LeadnewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok("保存成功!");
    }
    @PutMapping("/update")
    public ResponseResult updateSensitive(@RequestBody AdSensitive adSensitive){
        //if (adSensitive.getId() == null){
        //    throw new LeadnewsException(AppHttpCodeEnum.SERVER_ERROR);
        //}
        adSensitive.setCreatedTime(new Date());
        boolean b = adSensitiveService.updateById(adSensitive);
        if (!b){
            throw new LeadnewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok("修改成功!");

    }
    @DeleteMapping("del/{id}")
    public ResponseResult deleteSensitive(@PathVariable(name = "id")Integer id){
        boolean b = adSensitiveService.removeById(id);
        if (!b){
            throw new LeadnewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok("删除成功!");
    }

}

